/*
* $Id: JournalForm.java 270 2011-02-20 13:59:40Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2011 by Volokitin Alexey
* All rights reserved
*
*/
package apiary.ui.journal;

import apiary.common.FormHelper;
import java.util.*;
import javax.microedition.lcdui.*;

import apiary.Apiary;
import apiary.component.journal.Wizard;
import apiary.db.JournalStore.JournalRecord;
import apiary.db.managers.DBManager;
import apiary.settings.*;
import apiary.settings.SettingsRecord.Field;
import apiary.ui.*;

public class JournalForm extends UserFieldForm implements ItemEventListener,
                                                          ItemStateListener
{
    public class JournalValue
    {
        public Date date;
        public int frame;
        public int strength;
        public int isQB;
        public int wax;
        public int ambrosia;
        public int evacuation;
        public int dieting;
        public int disease;
        public String note;
        public Vector userFields;

        public JournalValue() {
            date = new Date();
            note = new String();
            userFields = new Vector();
        }
    }

    private Command chooseCommand;
    private CategoryItem curCategory;

    public JournalForm(String name, Wizard wiz, Integer id, boolean nextCommand) {
        super(name, wiz, id, nextCommand);
        Apiary apiary = Apiary.getInstance();
        DateField dateItem = new DateField(apiary.getLocaleString("DateInspect"), DateField.DATE);
        dateItem.setDate(new Date());
        append(dateItem);
        append(new TextField(apiary.getLocaleString("CountFrame"), "0", 256, TextField.NUMERIC));
        append(new TextField(apiary.getLocaleString("Strength"), "0", 256, TextField.NUMERIC));
        append(apiary.getLocaleString("IsQueenBee"));
        CategoryItem isQB = FormHelper.createCategoryItem(this, "isQB", "", "");
        DBManager dBManager = DBManager.getInstance();
        Hashtable qbState = dBManager.getQBStates();
        isQB.setName((String) qbState.get(new Integer(JournalRecord.IS_DEFAULT)));
        isQB.setState("0");
        append(isQB);
        SettingStore settings = SettingStore.getInstance();
        if(settings.getData(SettingsRecord.GIVE_WOX).getValue() == Boolean.TRUE)
            append(new TextField(apiary.getLocaleString("GiveWox"), "0", 256, TextField.NUMERIC));
        if(settings.getData(SettingsRecord.AMBROSIA).getValue() == Boolean.TRUE)
            append(new TextField(apiary.getLocaleString("Ambrosia"), "0", 256, TextField.NUMERIC));
        if(settings.getData(SettingsRecord.DISEASES).getValue() == Boolean.TRUE) {
            append(apiary.getLocaleString("Diseases"));
            CategoryItem disease = FormHelper.createCategoryItem(this, "disease", "", "");
            Hashtable diseases = dBManager.getDiseases();
            disease.setName((String) diseases.get(new Integer(JournalRecord.DISEASE_HEALTHY)));
            disease.setState("0");
            append(disease);
        }
        if(settings.getData(SettingsRecord.FEEDING).getValue() == Boolean.TRUE)
            append(new TextField(apiary.getLocaleString("Feeding"), "0", 256, TextField.NUMERIC));
        if(settings.getData(SettingsRecord.PUMP_HONEY).getValue() == Boolean.TRUE)
            append(new TextField(apiary.getLocaleString("PumpHoney"), "0", 256, TextField.NUMERIC));
        if(settings.getData(SettingsRecord.NOTE_INSPECT).getValue() == Boolean.TRUE)
            append(new TextField(apiary.getLocaleString("Note"), "", 256, TextField.ANY));
        appendUserFields();
        
        setItemStateListener(this);
    }

    protected void commandUser(Command command) {
        itemNotifyEvent(curCategory, PRESS);
    }

    public void setJournalValue(JournalValue journal) {
        int beginUser = 5;
        DateField date = (DateField) get(0);
        date.setDate(journal.date);
        TextField text = (TextField) get(1);
        text.setString(String.valueOf(journal.frame));
        text = (TextField) get(2);
        text.setString(String.valueOf(journal.strength));
        CategoryItem item = (CategoryItem) get(4);
        Hashtable states = DBManager.getInstance().getQBStates();
        String state = (String) states.get(new Integer(journal.isQB));
        item.setName(state);
        item.setState(String.valueOf(journal.isQB));
        SettingStore settings = SettingStore.getInstance();
        Field field = (Field) settings.getData(SettingsRecord.GIVE_WOX);
        if(field.getValue() == Boolean.TRUE) {
            text = (TextField) get(beginUser);
            text.setString(String.valueOf(journal.wax));
            beginUser++;
        }
        field = (Field) settings.getData(SettingsRecord.AMBROSIA);
        if(field.getValue() == Boolean.TRUE) {
            text = (TextField) get(beginUser);
            text.setString(String.valueOf(journal.ambrosia));
            beginUser++;
        }
        field = (Field) settings.getData(SettingsRecord.DISEASES);
        if(field.getValue() == Boolean.TRUE) {
            item = (CategoryItem) get(beginUser + 1);
            Hashtable diseases = DBManager.getInstance().getDiseases();
            String disease = (String) diseases.get(new Integer(journal.disease));
            item.setName(disease);
            item.setState(String.valueOf(journal.disease));
            beginUser += 2;
        }
        field = (Field) settings.getData(SettingsRecord.FEEDING);
        if(field.getValue() == Boolean.TRUE) {
            text = (TextField) get(beginUser);
            text.setString(String.valueOf(journal.dieting));
            beginUser++;
        }
        field = (Field) settings.getData(SettingsRecord.PUMP_HONEY);
        if(field.getValue() == Boolean.TRUE) {
            text = (TextField) get(beginUser);
            text.setString(String.valueOf(journal.evacuation));
            beginUser++;
        }
        field = (Field) settings.getData(SettingsRecord.NOTE_INSPECT);
        if(field.getValue() == Boolean.TRUE) {
            text = (TextField) get(beginUser);
            text.setString(journal.note);
            beginUser++;
        }
        setUserFieldsValue(beginUser, journal.userFields);
    }

    public Object GetDataForm() {
        int beginUser = 5;
        JournalValue journal = new JournalValue();
        DateField date = (DateField) get(0);
        journal.date = date.getDate();
        TextField text = (TextField) get(1);
        journal.frame = Integer.parseInt(text.getString());
        text = (TextField) get(2);
        journal.strength = Integer.parseInt(text.getString());
        CategoryItem item = (CategoryItem) get(4);
        journal.isQB = Integer.parseInt(item.getState());
        SettingStore settings = SettingStore.getInstance();
        Field field = (Field) settings.getData(SettingsRecord.GIVE_WOX);
        if(field.getValue() == Boolean.TRUE) {
            text = (TextField) get(beginUser);
            journal.wax = Integer.parseInt(text.getString());
            beginUser++;
        }
        field = (Field) settings.getData(SettingsRecord.AMBROSIA);
        if(field.getValue() == Boolean.TRUE) {
            text = (TextField) get(beginUser);
            journal.ambrosia = Integer.parseInt(text.getString());
            beginUser++;
        }
        field = (Field) settings.getData(SettingsRecord.DISEASES);
        if(field.getValue() == Boolean.TRUE) {
            item = (CategoryItem) get(beginUser + 1);
            journal.disease = Integer.parseInt(item.getState());
            beginUser += 2;
        }
        field = (Field) settings.getData(SettingsRecord.FEEDING);
        if(field.getValue() == Boolean.TRUE) {
            text = (TextField) get(beginUser);
            journal.dieting = Integer.parseInt(text.getString());
            beginUser++;
        }
        field = (Field) settings.getData(SettingsRecord.PUMP_HONEY);
        if(field.getValue() == Boolean.TRUE) {
            text = (TextField) get(beginUser);
            journal.evacuation = Integer.parseInt(text.getString());
            beginUser++;
        }
        field = (Field) settings.getData(SettingsRecord.NOTE_INSPECT);
        if(field.getValue() == Boolean.TRUE) {
            text = (TextField) get(beginUser);
            journal.note = text.getString();
            beginUser++;
        }
        journal.userFields = getUserFieldsValue(beginUser);
        return journal;
    }

    public void itemNotifyEvent(Item item, String event) {
        if(event.equals(PRESS)){
            Apiary apiary = Apiary.getInstance();
            String name = "";
            CategoryItem categoryItem = (CategoryItem) item;
            if(categoryItem.getId().equals("isQB"))
                name = apiary.getLocaleString("IsQueenBee");
            else if(categoryItem.getId().equals("disease"))
                name = apiary.getLocaleString("Diseases");
            ChooseList list = new ChooseList(name, item);
            if(categoryItem.getId().equals("isQB"))
                fillQBState(list);
            else if(categoryItem.getId().equals("disease"))
                fillDiseases(list);
            list.setBackScreen(this);
            apiary.Display(list);
        }
    }

    public void itemStateChanged(Item item) {
        if(item.getClass() != CategoryItem.class)
            return;
        CategoryItem categoryItem = (CategoryItem) item;
        Apiary apiary = Apiary.getInstance();
        if(categoryItem.getActive()) {
            chooseCommand = new Command(apiary.getLocaleString("Choose"), Command.ITEM, 0);
            addCommand(chooseCommand);
            curCategory = categoryItem;
        } else {
            removeCommand(chooseCommand);
            curCategory = null;
        }
    }

    private void fillQBState(ChooseList list) {
        Apiary apiary = Apiary.getInstance();
        Hashtable qbStates = DBManager.getInstance().getQBStates();
        Enumeration statesEnum = qbStates.keys();
        while(statesEnum.hasMoreElements()) {
            Integer state = (Integer) statesEnum.nextElement();
            String stateName = (String) qbStates.get(state);
            CategoryItem item = FormHelper.createCategoryItem(list,
                    String.valueOf(state), stateName, "");
            list.insert(0, item);
        }
    }

    private void fillDiseases(ChooseList list) {
        Apiary apiary = Apiary.getInstance();
        Hashtable diseases = DBManager.getInstance().getDiseases();
        Enumeration disEnum = diseases.keys();
        while(disEnum.hasMoreElements()) {
            Integer disease = (Integer) disEnum.nextElement();
            String disName = (String) diseases.get(disease);
            CategoryItem item = FormHelper.createCategoryItem(list,
                    String.valueOf(disease), disName, "");
            list.insert(0, item);
        }
    }
}
